package com.smartwebee.android.blespp.blockly;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.smartwebee.android.blespp.BluetoothLeService;
import com.smartwebee.android.blespp.R;
import com.smartwebee.android.blespp.blockly.base.BlockBaseActivity;
import com.smartwebee.android.blespp.blockly.command.CommandH5Delegate;
import com.smartwebee.android.blespp.hospital.BaseActivity;

import static com.smartwebee.android.blespp.BleSppActivity.EXTRAS_DEVICE_ADDRESS;
import static com.smartwebee.android.blespp.BleSppActivity.EXTRAS_DEVICE_NAME;

public class BlockTestActivity extends BlockBaseActivity implements View.OnClickListener {
    //获取buf数组的长度
    long recv_cnt = 0;
    private StringBuilder mData;

    //显示硬件返回组件
    private TextView tvDataRead;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_block_test);
        initView();
    }

    public void initView() {
        mData = new StringBuilder();
        tvDataRead = findViewById(R.id.tv_data_read);
        findViewById(R.id.btn_back).setOnClickListener(this);
        findViewById(R.id.btn_on).setOnClickListener(this);
        findViewById(R.id.btn_off).setOnClickListener(this);
        findViewById(R.id.btn_spin).setOnClickListener(this);
    }

    @Override
    public void displayData(byte[] buf) {
        recv_cnt += buf.length;

        if (recv_cnt >= 1024) {
            recv_cnt = 0;
            mData.delete(0, mData.length() / 2); //UI界面只保留512个字节，免得APP卡顿
        }

        String s = BlocklyUtils.asciiToString(buf);
        mData.append(s);
        tvDataRead.setText(mData.toString());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
            case R.id.btn_on:
                deleteText();
                sendDataToDevice(CommandH5Delegate.INSTANCE().getLedStr("全部", "12", "234", "345"));
                break;
            case R.id.btn_off:
                deleteText();
                sendDataToDevice(CommandH5Delegate.INSTANCE().getLedStr("左", "12", "234", "345"));
                break;
            case R.id.btn_spin:
//                showPrompt();
                deleteText();
                sendDataToDevice(CommandH5Delegate.INSTANCE().getBeepStr("C5", "0.5"));
                break;
        }
    }

    private void deleteText() {
        mData.delete(0, mData.length());
        tvDataRead.setText(mData.toString());
    }

    private void showPrompt() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请输入转动频率");//设置对话框标题
        final EditText edit = new EditText(this);
        edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
        builder.setView(edit);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (edit.getText().toString().trim().length() > 0) {
                    deleteText();
                    sendDataToDevice(BlockTestCommand.getSpinStr(edit.getText().toString().trim()));
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.show();
    }
}
